﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data;

namespace ef.DataBase
{
    /// <summary>
    /// 数据库基类
    /// </summary>
    public abstract class DataBase
    {
        public DataBase()
        {
        }
        /// <summary>
        /// 数据库访问对象
        /// </summary>
        protected DataContext Db = new DataContext();

        /// <summary>
        /// 设置实体对象的状态
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="state"></param>
        protected void SetStatus(object entity, EntityState state)
        {
            Db.Entry(entity).State = state;
        }

        /// <summary>
        /// 统一提交动作
        /// </summary>
        protected virtual void SubmitChanges()
        {
            try
            {
                Db.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        #region insert,delete,update,select 操作

        protected void Update<TEntity>(TEntity entity) where TEntity : Entity.BaseEntity
        {
            this.SetStatus(entity, EntityState.Modified);
            this.SubmitChanges();
        }

        protected void Update<TEntity>(IList<TEntity> list) where TEntity : Entity.BaseEntity
        {
            list.ToList().ForEach(entity =>
            {
                this.SetStatus(entity, EntityState.Modified);
            });
            this.SubmitChanges();
        }

        protected void Insert<TEntity>(TEntity entity) where TEntity : Entity.BaseEntity
        {

            this.SetStatus(entity, EntityState.Added);
            this.SubmitChanges();
        }

        protected void Insert<TEntity>(IList<TEntity> list) where TEntity : Entity.BaseEntity
        {
            list.ToList().ForEach(entity =>
            {
                this.SetStatus(entity, EntityState.Added);
            });
            this.SubmitChanges();
        }

        protected TEntity InsertGetIDENTITY<TEntity>(TEntity entity) where TEntity : Entity.BaseEntity
        {
            this.SetStatus(entity, EntityState.Added);
            this.SubmitChanges();
            return GetModel<TEntity>().Where(i => i.Equals(entity)).FirstOrDefault();
        }

        protected void Delete<TEntity>(TEntity entity) where TEntity : Entity.BaseEntity
        {
            this.SetStatus(entity, EntityState.Deleted);
            this.SubmitChanges();
        }

        protected IQueryable<TEntity> GetModel<TEntity>() where TEntity : Entity.BaseEntity
        {

            return Db.Set<TEntity>();
        }

        protected TEntity GetModelById<TEntity>(long id) where TEntity : Entity.BaseEntity
        {
            return GetModel<TEntity>().FirstOrDefault(i => i.ID.Equals(id));
        }

        #endregion
    }
}
